﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string Station_Add(Station model ,int ACID);


[OperationContract]
string Station_Update(Station model ,int ACID);


[OperationContract]
string StationByStationCode_Delete(string StationCode,int ACID);


[OperationContract]
Station StationByStationCode_Get(string StationCode,int ACID );


[OperationContract]
List<Station> Station_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加Station表数据
/// <summary>
/// 添加Station
/// </summary>
/// <param name="model">Station实体</param>
/// <returns>空或错误</returns>
public string Station_Add(Station model,int ACID)
{
if(string.IsNullOrEmpty(model.StationCode))
{
return "『岗位编码』不能为空！";
}
if(string.IsNullOrEmpty(model.StationName))
{
return "『岗位名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Station.Count(p=>p.StationCode==model.StationCode)>0)
{
return "主键已存在！";
}
ctx.Station.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加Station表数据事物用重构
/// <summary>
/// 添加Station事物用重构
/// </summary>
/// <param name="model">Station实体</param>
/// <returns>空或错误</returns>
private string Station_Add(Station model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.StationCode))
{
return "『岗位编码』不能为空！";
}
if(string.IsNullOrEmpty(model.StationName))
{
return "『岗位名称』不能为空！";
}
if(ctx.Station.Count(p=>p.StationCode==model.StationCode)>0)
{
return "主键已存在！";
}
ctx.Station.InsertOnSubmit(model);
return string.Empty;
}
#endregion

#region  修改Station表数据
/// <summary>
/// 修改Station
/// </summary>
/// <param name="model">Station实体</param>
/// <returns>空或错误</returns>
public string Station_Update(Station model,int ACID)
{
if(string.IsNullOrEmpty(model.StationCode))
{
return "『岗位编码』不能为空！";
}
if(string.IsNullOrEmpty(model.StationName))
{
return "『岗位名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Station.Count(p=>p.StationCode==model.StationCode)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
Station Stationmodel=ctx.Station.Single(p => p.StationCode==model.StationCode);
if(!string.IsNullOrEmpty(model.StationCode))
{
Stationmodel.StationCode=model.StationCode;
}
if(!string.IsNullOrEmpty(model.StationName))
{
Stationmodel.StationName=model.StationName;
}
if(!string.IsNullOrEmpty(model.powers))
{
Stationmodel.powers=model.powers;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Station表ByStationCode数据
/// <summary>
/// 删除StationByStationCode
/// </summary>
/// <param name="StationCode">岗位编码</param>
/// <returns>空或错误</returns>
public string StationByStationCode_Delete(string StationCode,int ACID)
{
if(string.IsNullOrEmpty(StationCode))
{
return "『岗位编码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Station.Count(p=>p.StationCode==StationCode)!=1)
{
return null;
}
Station model=ctx.Station.Single(p => p.StationCode== StationCode);
ctx.Station.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Station表ByStationCode数据[事物用重构]
/// <summary>
/// 删除StationByStationCode[事物用重构]
/// </summary>
/// <param name="StationCode">岗位编码</param>
/// <returns>空或错误</returns>
private string StationByStationCode_Delete(string StationCode,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(StationCode))
{
return "『岗位编码』不能为空！";
}
if(ctx.Station.Count(p=>p.StationCode==StationCode)!=1)
{
return null;
}
Station model=ctx.Station.Single(p => p.StationCode== StationCode);
ctx.Station.DeleteOnSubmit(model);
return string.Empty;
}
#endregion

#region  获取Station表ByStationCode数据
/// <summary>
/// 获取StationByStationCode
/// </summary>
/// <param name="StationCode">岗位编码</param>
/// <returns>当前表</returns>
public Station StationByStationCode_Get(string StationCode,int ACID)
{
if(string.IsNullOrEmpty(StationCode))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Station.Count(p=>p.StationCode==StationCode)!=1)
{
return null;
}
return ctx.Station.SingleOrDefault(p=>p.StationCode==StationCode);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion

#region  获取Station表数据
/// <summary>
/// 获取Station
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Station></returns>
public List<Station> Station_Get(Expression<Func<Station, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Station.ToList();
}
 else 
{
return ctx.Station.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Station.Where(predicate).ToList();
}
 else 
{
return ctx.Station.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取Station表数据
/// <summary>
/// 获取Station
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Station></returns>
public List<Station> Station_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return Station_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取Station表数据计数
/// <summary>
/// 获取Station表数据计数
/// </summary>
/// <returns>List<Station></returns>
public int Station_GetCount(Expression<Func<Station, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.Station.Count();

}
 else
{
return ctx.Station.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取Station分页表数据
/// <summary>
/// 获取Station分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Station></returns>
public List<Station> Station_Getpage(int pageIndex, int pageSize,Expression<Func<Station, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Station.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Station.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Station.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Station.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改Station表数据
/// <summary>
/// 批量修改Station
/// </summary>
/// <param name="predicate">Expression<Func<Station, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Station,Station>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int Station_batchUpdate(Expression<Func<Station, bool>> predicate,Expression<Func<Station, Station>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Station.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改Station表数据事物用重构
/// <summary>
/// 批量修改Station事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Station, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Station,Station>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Station_batchUpdate(Expression<Func<Station, bool>> predicate,Expression<Func<Station, Station>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Station.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除Station表数据
/// <summary>
/// 批量删除Station
/// </summary>
/// <param name="predicate">Expression<Func<Station, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int Station_batchDel(Expression<Func<Station, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Station.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除Station表数据事物用重构
/// <summary>
/// 批量删除Station事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Station, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Station_batchDel(Expression<Func<Station, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Station.Delete(predicate);
return ok;
}
#endregion


}
}

